﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace publicLibrary
{
    public static class myHtmlHelper
    {
        /// <summary>
        /// 返回路由值
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="key">area controller action id</param>
        /// <returns></returns>
        public static String getRoute(this HtmlHelper htmlHelper, routeKeyEnum routeKey)
        {
            return htmlHelper.ViewContext.RouteData.Values[routeKey.ToString()].ToTryString();
        }

        /// <summary>
        /// 生成 下拉列表,单选,多选
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="domName"></param>
        /// <param name="selectList"></param>
        /// <param name="selectType"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static MvcHtmlString myHtmlDom(this HtmlHelper htmlHelper, string domName, Dictionary<string, string> selectList, selectType selectType, string[] defValue = null)
        {
            List<string> re = new List<string>();
            int i = 0;
            defValue = defValue ?? new string[]{};

            switch (selectType)
            {
                case selectType.下拉列表:
                    re.Add(string.Format(@"<select id=""{0}"" name=""{0}"">", domName));
                    foreach (var item in selectList)
                    {
                        re.Add(string.Format(@"<option value=""{0}"" {2}>{1}</option>", item.Key, item.Value, defValue.Contains(item.Key) ? @"selected=""selected""" : ""));
                    }
                    re.Add(@"</select>");
                    break;
                case selectType.单选:
                    foreach (var item in selectList)
                    {
                        i++;
                        re.Add(string.Format(@"<input type=""radio"" id=""{0}"" name=""{1}"" value=""{2}"" {4}/><label for=""{0}"">{3}<label>", domName + i.ToString(), domName, item.Key, item.Value, defValue.Contains(item.Key) ? @"checked=""checked""" : ""));
                    }
                    break;
                case selectType.多选:
                    foreach (var item in selectList)
                    {
                        i++;
                        re.Add(string.Format(@"<input type=""checkbox"" id=""{0}"" name=""{1}"" value=""{2}"" {4}/><label for=""{0}"">{3}<label>", domName + i.ToString(), domName, item.Key, item.Value, defValue.Contains(item.Key) ? @"checked=""checked""" : ""));
                    }
                    break;
            }

            return new MvcHtmlString(string.Join("\n", re.ToArray()));
        }

        public static MvcHtmlString myHtmlDom(this HtmlHelper htmlHelper, string domName, Dictionary<string, string> selectList, selectType selectType, string defValue = null)
        {
            return htmlHelper.myHtmlDom(domName, selectList, selectType, new string[] { defValue ?? "" });
        }
    }

    public enum selectType
    {
        下拉列表,
        单选,
        多选
    }

    public enum routeKeyEnum
    {
        controller,
        action,
        area,
        id
    }

}